<?php
namespace app\user\controller;
use think\Input;
class Index extends Common{
    public function initialize(){
        parent::initialize(); 
    }
    public function index(){
        if(isMobile()) $this->redirect('main');
        if($this->userInfo['level']!=1){
            $this->assign('title','【代理】码商中心');
            $userlisturl=url('user/agent/index');
            $orderlisturl=url('user/agent/orderlist'); 
            $this->assign('dailinav','<li><a href="'.$userlisturl.'" target="main_pan"><i class="layui-icon">&#xe66f;</i> 下级码商</a></li><li><a href="'.$orderlisturl.'" target="main_pan"><i class="layui-icon">&#xe66e;</i> 码商订单</a></li>');  
        }else{
            $this->assign('title','码商中心');
            $this->assign('dailinav',NULL); 
        }  
        return $this->fetch();
    } 
    public function main(){ 
        $userid=session('user.id');
        $cdata['order_no']=db('channel_order')->where('state','<',2)->where('userid',$userid)->count();
        $cdata['order_all']=db('channel_order')->where('userid',$userid)->count();
        $cdata['order_money_pay']=db('jiesuan')->where('userid',$userid)->sum('money_s');
        $cdata['order_money']=db('channel_order')->where('state',2)->where('userid',$userid)->sum('money');
        $cdata['channel_yes']=db('channel')->where('open',1)->where('userid',$userid)->count();
        $cdata['channel_all']=db('channel')->where('userid',$userid)->count();
        if($this->userInfo['agentid']){
            $agent=get_user_info($this->userInfo['agentid'],0);
            $agentname=$agent['username'];
        }else{
            $agentname=$this->sys['name'];
        }
        if($this->userInfo['level']!=1){ 
            $userlisturl=url('user/agent/index');
            $orderlisturl=url('user/agent/orderlist'); 
            $this->assign('dailinav','<li><a href="'.$userlisturl.'"><i class="layui-icon">&#xe66f;</i> 下级码商</a></li><li><a href="'.$orderlisturl.'"><i class="layui-icon">&#xe66e;</i> 码商订单</a></li>');  
        }else{ 
            $this->assign('dailinav',NULL); 
        }  
        $this->assign('agentname',$agentname);
        $this->assign('cdata',$cdata);
        $this->assign('title','主页');
        return $this->fetch(); 
    } 
    public function userstrade(){
        $userid=input('post.userid'); 
        $state=input('post.state'); 
        if($state==1){
            $state=0; 
        }else{
            $state=1;  
        } 
        if(db('users')->where('id',$userid)->update(['state'=>$state])!==false){
            db('channel')->where('userid',$userid)->update(array('state'=>$state));  
            return ['status'=>1,'msg'=>'设置成功!'];
        }else{
            return ['status'=>0,'msg'=>'设置失败!'];
        }
    }
    public function usersvoice(){
        $userid=input('post.userid'); 
        $voice=input('post.voice'); 
        if($voice=='open'){
            $voice='close'; 
        }else{
            $voice='open';  
        } 
        if(db('users')->where('id',$userid)->update(['voice'=>$voice])!==false){ 
            return ['status'=>1,'msg'=>'设置成功!'];
        }else{
            return ['status'=>0,'msg'=>'设置失败!'];
        }
    }
}